package cn.edu.zsc.blms.service.array;

import java.text.ParseException;
import java.util.Date;
import cn.edu.zsc.blms.App;
import cn.edu.zsc.blms.domain.Book;
import cn.edu.zsc.blms.domain.Reader;
import cn.edu.zsc.blms.exception.EntityNotExistException;
import cn.edu.zsc.blms.service.IBookService;
import cn.edu.zsc.blms.service.IReaderService;
import cn.edu.zsc.blms.util.DateUtil;

public class BookService extends BaseService implements IBookService {
    private static IBookService singleton;
    private BookService(){}
    public static IBookService getSingleton(){
        if(singleton == null)singleton = new BookService();
        return singleton;
    }
    @Override
    public void create(){
        //创建图书
        if(Book.idCounter >= DATA.length){
            System.out.println("图书数量已满, 不能再添加");
            return;
        }
        System.out.println("请输入书名: ");
        String name = App.input.nextLine();
        System.out.println("请输入书号: ");
        String isbn = App.input.nextLine();
        System.out.println("请输入类目: ");
        String category = App.input.nextLine();
        System.out.println("请输入作者: ");
        String author = App.input.nextLine();
        System.out.println("请输入出版社: ");
        String press = App.input.nextLine();
        System.out.println("请输入出版日期,格式为yyyy-MM-dd: ");
        Date publishDate = null;
        while(publishDate == null){
            String strDate = App.input.nextLine();
            try {
                publishDate = DateUtil.parseDate(strDate);
            }catch (ParseException e){
                System.out.print("输入的日期格式错误, 请重新输入: ");
            }
        }
        System.out.println("请输入单价: ");
        Double price = null;
        while(price == null){
            String strPrice = App.input.nextLine();
            try{
                price = Double.parseDouble(strPrice);
            }catch (NumberFormatException e){
                System.out.print("输入的单价格式错误, 请重新输入: ");
            }
        }
        Book book = new Book(name, isbn, category, author, press,  publishDate, price);
        DATA[book.id] = book;
        System.out.println("成功添加1本图书");
    }

    @Override
    public void disable(){
        //废弃图书
        System.out.println("请输入要禁用的图书ID: ");
        try {
            Book book = (Book) getEntity();
            book.disabled = true;
            System.out.println("图书" + book.id + "已被废弃");
        }catch (EntityNotExistException e){
            System.out.println(e.getMessage());
        }
    }
    @Override
    public void update(){
        //修改图书
        System.out.println("请输入要修改的图书ID: ");
        try {
            Book book = (Book) getEntity();
            System.out.println("请输入书名, 直接按回车表示不修改: ");
            String name = App.input.nextLine();
            book.name = "".equals(name) ? book.name : name;
            System.out.println("请输入书号, 直接按回车表示不修改: ");
            String isbn = App.input.nextLine();
            book.isbn = "".equals(isbn) ? book.isbn : isbn;
            System.out.println("请输入类目, 直接按回车表示不修改: ");
            String category = App.input.nextLine();
            book.category = "".equals(category) ? book.category : category;
            System.out.println("请输入作者, 直接按回车表示不修改: ");
            String author = App.input.nextLine();
            book.author = "".equals(author) ? book.author : author;
            System.out.println("请输入出版社, 直接按回车表示不修改: ");
            String press = App.input.nextLine();
            book.press = "".equals(press) ? book.press : press;
            System.out.println("请输入出版日期,格式为yyyy-MM-dd, 直接按回车表示不修改: ");
            Date publishDate = null;
            while (publishDate == null) {
                String strDate = App.input.nextLine();
                if ("".equals(strDate)) {
                    publishDate = book.publishDate;
                    break;
                }
                try {
                    publishDate = DateUtil.parseDate(strDate);
                } catch (ParseException e) {
                    System.out.print("输入的日期格式错误, 请重新输入: ");
                }
            }
            book.publishDate = publishDate;
            System.out.println("请输入单价, 直接按回车表示不修改: ");
            Double price = null;
            while (price == null) {
                String strPrice = App.input.nextLine();
                if ("".equals(strPrice)) {
                    price = book.price;
                    break;
                }
                try {
                    price = Double.parseDouble(strPrice);
                } catch (NumberFormatException e) {
                    System.out.print("输入的单价格式错误, 请重新输入: ");
                }
            }
            book.price = price;
            System.out.println("图书" + book.id + "的信息已被修改");
        } catch (EntityNotExistException e){
            System.out.println(e.getMessage());
        }
    }
    @Override
    public void query(){
        //查询图书
        System.out.println("请输入要查询的图书名称: ");
        String name = App.input.nextLine();
        for(int i = 0;i < Book.idCounter; ++i){
            Book book = (Book)DATA[i];
            if(book.name != null && book.name.contains(name)){
                System.out.println(book);
            }
        }
    }
}
